package ext.tianma.part.finishedProduct.util;

import java.util.List;
import java.util.Vector;

import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.WTObject;
import wt.httpgw.URLFactory;
import wt.inf.container.WTContained;
import wt.inf.container.WTContainer;
import wt.inf.team.ContainerTeam;
import wt.inf.team.ContainerTeamHelper;
import wt.inf.team.ContainerTeamManaged;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.project.Role;
import wt.util.WTException;
import wt.workflow.work.WfAssignedActivity;
import ext.tianma.part.util.emailService.SealedSampleEmail;

public class FinishProjectUtil {
	
	//ext.tianma.part.finishedProduct.util.FinishProjectUtil.getMaintainAttrsUrl();
	public static String getMaintainAttrsUrl(ObjectReference self, Persistable pbo,String roleName) throws Exception {
		String baseHref = new URLFactory().getBaseHREF();
		WTPart part = (WTPart) pbo;
		
		WfAssignedActivity wfaa = (WfAssignedActivity) self.getObject();
		
		/**
		 * add by xucr ： 
		 * 
		 * 此方法在工作流模板中节点【开始】时调用，此刻由于还没有产生workitem，因此将节点oid保存在链接中
		 * 
		 * */
		String activityOid = "OR:wt.workflow.work.WfAssignedActivity:" + wfaa.getPersistInfo().getObjectIdentifier().getId();
		
		String url = "<a href=\"Javascript:var processWindow = window.open('" + baseHref
				+ "ptc1/tianma/parts/editFinishedGoodAttrs?"
				+ "wizardActionClass=ext.tianma.part.finishedProduct.processors.EditFinishedGoodAttrsProcessors&"
				+ "wizardActionMethod=execute&actionName=editFinishedGoodAttrs&oid=VR%3Awt.part.WTPart%3A" + part.getBranchIdentifier()
				+ "&roleName="+roleName
				+ "&activityOid="+activityOid
				+ "', '_blank', 'resizable=no,scrollbars=yes,menubar=no,toolbar=no,location=no,status=no');\">"
				+ "成品属性" + "</a>";
		return url;
	}
	
	//ext.tianma.part.finishedProduct.util.FinishProjectUtil.getImportBOMUrl();
	public static String getImportBOMUrl(ObjectReference self, Persistable pbo) throws Exception {
		String baseHref = new URLFactory().getBaseHREF();
		String url = "<a href=\"Javascript:var processWindow = window.open('" + baseHref
				+ "ptc1/tianma/parts/importBOM?"
				+ "wizardActionClass=ext.tianma.part.processors.ImportBOMProcessors&"
				+ "wizardActionMethod=execute&actionName=importBOM"
				+ "', '_blank', 'resizable=no,scrollbars=yes,menubar=no,toolbar=no,location=no,status=no');\">"
				+ "导入BOM" + "</a>";
		return url;
	}
	
	//ext.tianma.part.finishedProduct.util.FinishProjectUtil.getImportBOMUrl();
	public static void sendEmailToAll(ObjectReference self, WTObject pbo) throws WTException{
		//WfProcess process = BomWorkFlowUtil.getProcess(self);
		WTContained obj = (WTContained)pbo;
		WTContainer container = obj.getContainer();

		ContainerTeamManaged ctm = null;
		if(container instanceof ContainerTeamManaged){
			ctm = (ContainerTeamManaged)container;
		}
		ContainerTeam containerteam = ContainerTeamHelper.service.getContainerTeam(ctm);
		Vector vector = containerteam.getRoles();

		String emailAddrs = "";
		for(int i = 0; i < vector.size(); i++) {	
			List tempUserList = containerteam.getAllPrincipalsForTarget((Role) vector.get(i));
			for (int j = 0; tempUserList != null && j < tempUserList.size(); j++) {
				WTPrincipalReference wtprincipalreference = (WTPrincipalReference) tempUserList.get(j);
				WTUser wtUser = (WTUser) wtprincipalreference.getObject();
				emailAddrs = wtUser.getEMail() + ";";
			}
		}
		
		if(emailAddrs.length() > 0){
			emailAddrs = emailAddrs.substring(0, emailAddrs.length()-1);
		}
		String titleStr = "["+((WTPart)pbo).getNumber()+"]成品属性维护完成通知";
		String content = ((WTPart)pbo).getNumber()+" 各个人员属性已维护完毕";

		SealedSampleEmail.sendEmail(emailAddrs, content, titleStr);
	}
}
